<?
	include_once('header.php');

	include_once('sidebar.php');

	$edit = $_GET['edit'];
	$action = $_GET['action'];
	$delete = $_GET['delete'];
?>

		
		<div id="main">
<?
	if(!drizzo_user_is_allowed('pages-write') || drizzo_global_options('module_pages') != 1) {
		echo "<h2>Pages</h2><p>This module is not available.</p><br class=\"clear\" />";
		include_once('footer.php');
		exit();
	}
	if($delete) { // Delete a post
?>
			<h2>Delete a Page</h2>
<?
		if(!isset($_POST['delete_check'])) { // Display a confirmation
			$the_post = drizzo_get_the_post($delete);
			if(drizzo_post_permissions_check()) {
?>
			<p>Are you sure you want to send <? echo $the_post['title']; ?> to the TrashHeap?</p>
			<form action="pages.php?delete=<? echo $delete; ?>" method="post" name="deleter" id="deleter">
				<input type="hidden" name="delete_check" value="1"></input>
				<input type="hidden" name="delete_id" value="<? echo $delete; ?>"></input>
				<p><input type="button" onclick="history.go(-1)" value="No" class="submit"></input><input type="submit" name="submit" value="Yes" class="submit"></input></p>
<?			
			} else {
				echo "<p class=\"alert\">You do not have permission to delete this page.</p>";
			}
		} else { // Move the post to the trashheap
			$trashheap = drizzo_move_to_trashheap($_POST['delete_id']);
			if($trashheap) {
				echo "<p class=\"trash\">The post has been piled on the TrashHeap.</p>";
			} else {
				echo "<p class=\"alert\">There was a problem and the post could not be moved to the TrashHeap.<br />" . mysql_error() . "</p>";
			}
		}
	} // End Delete

	if($action == "publish") { // Save post to database
	
		 if($_POST) {
			$insert = drizzo_post_save();

			if($insert) { // If insert is succesful
				echo "<p class=\"save\">" . drizzo_db_unwrap($_POST['title']) . " has been saved.</p>";
			} else { // If insert fails
				echo "<p class=\"alert\">There has been a database error. " . drizzo_db_unwrap($_POST['title']) . " has not been saved.<br />" . mysql_error() . "</p>";
			}
		} else { // If there is no Post data
			echo "<p class=\"alert\">There is nothing to publish.</p>";
		} // End $_POST
	} // End publish

	if(!isset($edit) && ($action != "continue") && ($action != "new") && !$delete && ($action != "continue_new")) { // Default View

		if(!$action || ($action == "publish")) {
?>
			<h2>Current Pages</h2>
<?
			drizzo_pages_table();
		}

		if($action == "view_drafts") { // View drafts setup
?>
			<h2>Draft Pages</h2>
<?			
			drizzo_pages_table('draft');
		}
	}

		if(($action == "continue_new") && $_POST) {
			$insert = drizzo_post_save();
			if($insert) {
				echo "<p class=\"save\">" . drizzo_db_unwrap($_POST['title']) . " has been saved. You may continue editing.</p>";
			} else {
				echo "<p class=\"alert\">There has been a database error. " . drizzo_db_unwrap($_POST['title']) . " has not been saved.<br />" . mysql_error() . "</p>";
			}

			$the_post = drizzo_get_the_post($insert);
		} // End $action == continue_new
		
	if($edit) {
		if(($action == "continue") && $_POST) {
			$update = drizzo_post_save();
			if($update) {
				echo "<p class=\"save\">" . drizzo_db_unwrap($_POST['title']) . " has been saved. You may continue editing.</p>";
			} else {
				echo "<p class=\"alert\">There has been a database error. " . drizzo_db_unwrap($_POST['title']) . " could not been saved.<br />" . mysql_error() . "</p>";
			}
		}
		$the_post = drizzo_get_the_post($edit);
	}
	
	if($edit || (($action == "continue_new") && $_POST)) {
		if(!drizzo_post_permissions_check()) {
			echo "<p class=\"alert\">You do not have permission to edit this post.</p>";
		} else {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
				
				<label for="title">Title</label><input name="title" type="text" value="<? echo drizzo_post_title(); ?>"></input><br />

				<label for="content">Content</label><textarea name="content" id="post_content"><? echo drizzo_post_content_backend(); ?></textarea><br />

				<label for="tags">Tags<br /><small>(separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"><? echo drizzo_post_tags($edit, "comma", "false"); if(drizzo_post_tags($edit)) { echo ", "; }?></textarea><br />

				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>

				<label for="date">Date</label>	<? echo drizzo_date_dropdowns(); ?>

				<input type="hidden" name="ID" value="<? echo $edit; ?>">
				
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'pages.php?edit=<? echo drizzo_post_id(); ?>&action=continue'" value="Save &amp; Continue Editing" class="submit"></input><input type="submit" name="edit_submit" onclick="document.editor.action = 'pages.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>
			
			<div id="options">

				<h3 id="file_list_header">Uploaded Media</h3>
				<div id="confirm_image">
					<p>Do you want to insert a link to the image with a thumbnail or place the full image directly?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertImageLink('thumb')" title="Thumbnail Link">Thumbnail Link</a> | <a href="javascript:void(0)" onclick="insertImageLink('image')" title="Full Size">Full Size</a> | <a href="javascript:void(0)" onclick="$('confirm_image').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="confirm_embed">
					<p>Do you want to insert a download link to the file or embed it to play on the page?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertMediaLink('link')" title="Download Link">Download Link</a> | <a href="javascript:void(0)" onclick="insertMediaLink('embed')" title="Embed">Embed</a> | <a href="javascript:void(0)" onclick="$('confirm_embed').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="file_list_div">
					<script type="text/javascript" charset="utf-8">
						getFolderContents('drizzo-files');
					</script>
				</div>
				
				<? drizzo_post_authors_dropdown(); ?>

				<? drizzo_types_dropdown(); ?>

				<? drizzo_post_statuses_dropdown(); ?>

				<? drizzo_post_comment_statuses_dropdown(); ?>

				<? drizzo_post_parents_dropdown(); ?>

				<? drizzo_menu_name_input(); ?>
				
				<? drizzo_page_order_input(); ?>
				
				<? drizzo_post_permalink_name_input(); ?>
				
				<? drizzo_post_eval_content_dropdown(); ?>
				
				<? drizzo_edit_status(); ?>

				<? if($edit) { echo "<h3>Previous Revisions</h3>\n" . drizzo_warehouse_revisions_list(); } ?>

			</div>

			</form>
<?
		}
	} // End $edit || $action == continue_new

	if($action == "new") {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
	
				<label for="title">Title</label><input name="title" type="text" value=""></input><br />

				<label for="content">Content</label><textarea name="content" id="post_content"></textarea><br />

				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"></textarea><br />
	
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>

				<label for="date">Date</label>	<? echo drizzo_date_dropdowns(); ?>

				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'pages.php?action=continue_new'" value="Save Draft" class="submit"></input><input type="submit" name="new_submit" onclick="document.editor.action = 'pages.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>

			<div id="options">

				<h3 id="file_list_header">Uploaded Media</h3>
				<div id="confirm_image">
					<p>Do you want to insert a link to the image with a thumbnail or place the full image directly?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertImageLink('thumb')" title="Thumbnail Link">Thumbnail Link</a> | <a href="javascript:void(0)" onclick="insertImageLink('image')" title="Full Size">Full Size</a> | <a href="javascript:void(0)" onclick="$('confirm_image').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="confirm_embed">
					<p>Do you want to insert a download link to the file or embed it to play on the page?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertMediaLink('link')" title="Download Link">Download Link</a> | <a href="javascript:void(0)" onclick="insertMediaLink('embed')" title="Embed">Embed</a> | <a href="javascript:void(0)" onclick="$('confirm_embed').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="file_list_div">
					<script type="text/javascript" charset="utf-8">
						getFolderContents('drizzo-files');
					</script>
				</div>
<?
	
?>
				<? drizzo_post_authors_dropdown(session_user_id); ?>

				<? drizzo_types_dropdown(); ?>

				<? drizzo_post_statuses_dropdown(); ?>

				<? drizzo_post_comment_statuses_dropdown(); ?>

				<? drizzo_post_parents_dropdown(); ?>

				<? drizzo_menu_name_input(); ?>

				<? drizzo_page_order_input(); ?>

				<? drizzo_post_permalink_name_input(); ?>

				<? drizzo_post_eval_content_dropdown(); ?>

				<? drizzo_edit_status(); ?>

			</div>

			</form>
<?
	} // End $action == new
?>
			<br class="clear" />

		</div>
<?
	if($edit) {
?>
		<div id="preview_wrap">
			
			<div id="preview">
			
				<h2>Page Preview</h2>
			
				<? if(strlen($the_post['content'])>0) { echo drizzo_texturize($the_post['content']); } else { echo "<p>Save the post to view this preview.</p>"; } ?>

			</div>

		</div>
<?
	}
	include_once('footer.php');
?>
